package org.yxing.palpalgps.sqlite;

import org.yxing.palpalgps.models.ChatMessageModel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ChatDatabaseAdapter {
    private static final String TAG = "ChatDatabaseAdapter";
    
    private static final int DB_VERSION = 1;
    
    private static final String DB_NAME = "db_chat";
    
    private static final String DB_TABLE = "chat";
    
    private static final String KEY_ID = "_id";
    
    /**
     * current user id
     */
    private static final String KEY_UID = "uid";
    
    private static final String KEY_FROM = "from_id";
    
    private static final String KEY_TO = "to_id";
    
    private static final String KEY_MESSAGE = "message";
    
    private static final String KEY_TIMESTAMP = "timestamp";
    
    private static final String LIMIT = "20";
    
    private Context mContext;
    
    private DatabaseHelper mDatabaseHelper;
    
    private SQLiteDatabase mSQLiteDatabase;
    

    private class DatabaseHelper extends SQLiteOpenHelper {
        
        public DatabaseHelper() {
            super(mContext, DB_NAME, null, DB_VERSION);
        }

        public void onCreate(SQLiteDatabase db) {
            Log.d(TAG, "create table");
            db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
            
            String sql = "CREATE TABLE " + DB_TABLE + "(" 
                    + KEY_ID + " INTEGER PRIMARY KEY," 
                    + KEY_UID + " VARCHAR(64) NOT NULL,"
                    + KEY_FROM + " VARCHAR(64) NOT NULL," 
                    + KEY_TO + " VARCHAR(64) NOT NULL," 
                    + KEY_MESSAGE + " TEXT,"
                    + KEY_TIMESTAMP + " BIGINT"
                    + ")";
            
            db.execSQL(sql);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.d(TAG, "update table");
            onCreate(db);
        }
    }
    
    public ChatDatabaseAdapter(Context context) {
        mContext = context;
    }
    
    public void open() {
        mDatabaseHelper = new DatabaseHelper();
        mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
        Log.d(TAG, "open");
    }
    
    public void close() {
        mSQLiteDatabase.close();
        mDatabaseHelper.close();
        Log.d(TAG, "close");
    }
    
    /**
     * 
     * @param uid current user id
     * @param model ChatMessageModel
     * @return
     */
    public long insertData(String uid, ChatMessageModel model) {
        Log.v(TAG, "insert");
        ContentValues initiaValues = new ContentValues();
        initiaValues.put(KEY_UID, uid);
        initiaValues.put(KEY_FROM, model.getFrom());
        initiaValues.put(KEY_TO, model.getTo());
        initiaValues.put(KEY_MESSAGE, model.getEncodeBody());
        initiaValues.put(KEY_TIMESTAMP, model.getDate().getTime());
        
        return mSQLiteDatabase.insert(DB_TABLE, KEY_ID, initiaValues);
    }
    
    public Cursor fetchData(String uid, String fuid) {
        Log.v(TAG, "query");
        String[] columns = new String[]{KEY_ID, KEY_FROM, KEY_TO, KEY_MESSAGE, KEY_TIMESTAMP};
        String selection = KEY_UID + " = ? AND (" + KEY_FROM + "=? OR " + KEY_TO + "=?)";
        String[] selectionArgs = {uid, fuid, fuid};
        String orderBy = KEY_TIMESTAMP + " desc";
        
        return mSQLiteDatabase.query(DB_TABLE, columns, selection, selectionArgs, null, null, orderBy, LIMIT);
    }
    
    /**
     * clear data
     * @param fuid
     * @return
     */
    public int clearData(String fuid) {
        String whereClause = KEY_FROM + "=? OR " + KEY_TO + "=?";
        String whereArgs[] = new String[]{fuid, fuid};
        return mSQLiteDatabase.delete(DB_TABLE, whereClause, whereArgs);
    }
}
